<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            height: 3000px;
        }

        img {
            width: 200px;
            height: 200px;
            position: absolute;
            top: 100px;
            left: 50px;
            border: 5px solid #ffd2cf;
        }
    </style>
</head>

<body>
    <img id="img" src="./imgs/神子-表情包.jpg" alt="">

    <script>
        function scroll() {
            //兼容火狐
            if (window.pageYOffset) {
                return {
                    top: window.pageYOffset,
                    left: window.pageXOffset
                }
            }
            //兼容ie
            if (document.compatMode == "CSS1Compat") {
                return {
                    top: document.documentElement.scrollTop,
                    left: document.documentElement.scrollLeft
                }
            }
            //兼容谷歌
            return {
                top: document.body.scrollTop,
                left: document.body.scrollLeft
            }
        }

        window.onscroll = function () {
            if (scroll().top < 400) {
                body.style.background = "#abbcaa"
            } else if (scroll().top < 800) {
                body.style.background = "#abbacc"
            } else {
                body.style.background = "#accacc"
            }
        }

        var img = document.getElementById("img");
        var imgtop = img.offsetTop;
        var to = null;


        var leader = target = 0;
        window.onscroll = function () {
            target = imgtop + scroll().top;

            clearInterval(to);

            to = setInterval(function () {
                leader = leader + (target - leader) / 10;
                img.style.top = leader + "px"
            }, 10)
        }
    </script>
</body>

</html>